package com.vrp3d.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.vrp3d.common.enums.CommonEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 阿里云发送短信工具类
 *
 * @author vrp3d
 */
@Slf4j
@Component
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
public class AliSmsUtil {

    private static String accessKeyId;
    private static String accessSecret;
    private static String signName;
    private static String templateParamPre;
    private static String templateParamSuf;

    @Value("${ali.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        AliSmsUtil.accessKeyId = accessKeyId;
    }

    @Value("${ali.accessSecret}")
    public void setAccessSecret(String accessSecret) {
        AliSmsUtil.accessSecret = accessSecret;
    }

    @Value("${ali.signName}")
    public void setSignName(String signName) {
        AliSmsUtil.signName = signName;
    }

    @Value("${ali.templateParamPre}")
    public void setTemplateParamPre(String templateParamPre) {
        AliSmsUtil.templateParamPre = templateParamPre;
    }

    @Value("${ali.templateParamSuf}")
    public void setTemplateParamSuf(String templateParamSuf) {
        AliSmsUtil.templateParamSuf = templateParamSuf;
    }

    /**
     * 指定模板单发
     *
     * @param phone   手机号码
     * @param content 发送内容
     * @return 是否成功
     */
    public static boolean send(String phone, String content, String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParamPre + content + templateParamSuf);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            log.info("短信发送回执:{},签名:{},templateCode:{}", data, signName, templateCode);
            try {
                CommonResponseData commonResponseData = JSON.parseObject(data, CommonResponseData.class);
                return CommonEnum.ALIBABA_CLOUD_SMS_RECEIPT_CODE.getMsg().equals(commonResponseData.getCode());
            } catch (JSONException e) {
                log.error("解析阿里云短信回执json出错,jsonString:{}", data);
                return false;
            }
        } catch (ClientException e) {
            log.error("短信发送失败,手机号:{},内容:{},签名:{},模板:{}", phone, content, signName, templateCode + "_" + e.getMessage(), e);
            return false;
        }
    }

}
